package org.scboy.dbms.service.imp;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.dao.SqlEntityDao;
import org.scboy.dbms.service.SqlEntityService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.scboy.utils.enums.coobi.OrderType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hutool.core.lang.UUID;

@Service
public class SqlEntityServiceImpl implements SqlEntityService {

	@Autowired
	EntityFactory factory;
	
	@Override
	public Page page(Model params) {
		SqlEntityDao dao=factory.getDao(SqlEntityDao.class);
		params.orderBy("entVersion",OrderType.DESC);
		return dao.page(params);
	}

	@Override
	public boolean add(Model params) {
		SqlEntityDao dao=factory.getDao(SqlEntityDao.class);
		params.add("id", UUID.randomUUID().toString());
		int count= dao.insert(params);
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean update(Model params) {
		SqlEntityDao dao=factory.getDao(SqlEntityDao.class);
		int count= dao.update(params);
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public Model get(String id) {
		SqlEntityDao dao=factory.getDao(SqlEntityDao.class);
		return dao.get(Model.create().add("id", id));
	}

	@Override
	public boolean delete(String id) {
		SqlEntityDao dao=factory.getDao(SqlEntityDao.class);
		int count= dao.delete(Model.create().add("id",id));
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}

}
